package com.yonyou.pmclouds.completedcollate.processor;

import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.completedcollate.entity.CompletedCollateVO;
import com.yonyou.pmclouds.completedcollate.mapper.CompletedCollateVOMapper;

import java.util.List;

/**
 * 资料整理单据名称校验
 *
 * @author yzx
 * @Date: 2018/5/30 16:05
 */
public class CompletedCollateNameCheck implements SingleProcessor<CompletedCollateVO> {
    private CompletedCollateVOMapper collateVOMapper;

    public CompletedCollateNameCheck(CompletedCollateVOMapper collateVOMapper) {
        this.collateVOMapper = collateVOMapper;
    }

    @Override
    public void process(CompletedCollateVO vo, CompletedCollateVO orig) throws BusinessException {
        // 通过名字获取是否已经存在将要保存的目录名称
        List<CompletedCollateVO> collateVOS = collateVOMapper.queryByName(vo.getSname(), RuntimeEnvironment.getTenantId());
        if (collateVOS.size() > 0) {
            // 新增时如果存在同名
            if (vo.getPkCompletedCollate() == null) {
                throw new BusinessException("已经存在相同的名称，请修改!");
            } else {
                // 编辑时除了自己以外同名
                for (CompletedCollateVO collateVO : collateVOS) {
                    if (!vo.getPkCompletedCollate().equals(collateVO.getPkCompletedCollate())) {
                        throw new BusinessException("已经存在相同的名称，请修改!");
                    }
                }
            }
        }
    }
}
